﻿#include "single_book_add.h"
#include "ui_single_book_add.h"
#include "book_sql.h"
#include <QtDebug>
single_book_add::single_book_add(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::single_book_add)
{
    this->setAttribute(Qt::WA_DeleteOnClose);
    bok=new book_sql;
    QDateTime Time=QDateTime::currentDateTime();
    time = Time .toString("yyyy-MM-dd hh:mm:ss");//格式化时间


    ui->setupUi(this);

    ui->author->setPlaceholderText(QString("作者"));
    ui->bookName->setPlaceholderText(QString("书名"));
    ui->pageNum->setPlaceholderText(QString("页数"));
    ui->storePlace->setPlaceholderText(QString("存书位置"));
    ui->multi_book_in->setPlaceholderText(QString("存书数量"));

    connect(ui->author,&QLineEdit::returnPressed,this,&single_book_add::processRet);
    connect(ui->bookName,&QLineEdit::returnPressed,this,&single_book_add::processRet);
    connect(ui->multi_book_in,&QLineEdit::returnPressed,this,&single_book_add::processRet);
    connect(ui->storePlace,&QLineEdit::returnPressed,this,&single_book_add::processRet);
    connect(ui->pageNum,&QLineEdit::returnPressed,this,&single_book_add::processRet);
   // connect(ui->adding_multi_book,&QPushButton::click,this,&single_book_add::processRet);



}

single_book_add::~single_book_add()
{
    delete ui;
}

void single_book_add::on_adding_book_clicked()
{
    QDate date = QDate::currentDate();
    QString bookname=ui->bookName->text();
    QString book_author=ui->author->text();
    QString book_page=ui->pageNum->text();
    QString book_store=ui->storePlace->text();
    QString left_date =date.toString("yyyy-MM-dd");
    QString borrow = "allow";
    QString borrow_ID = "无人借阅";
    QString borrow_time = date.toString("yyyy-MM-dd");
    QTime time=QTime::currentTime();
    QString totaltime=date.toString("MMdd")+time.toString("mmss");
    int new_borrow= totaltime.toInt();
    QString new_book_num= QString::number(new_borrow+1);


    if(bookname=="" || book_author=="" || book_page==""|| book_store=="")

       { QMessageBox::information(this,tr("提示对话框"),tr("书名，作者，页数或存书位置输入为空，请检查并重新输入！"), QMessageBox::Ok);
        return;
    }
    book_info new_book (bookname,left_date,borrow,book_author,book_page,borrow_ID
                        ,book_store,borrow_time,new_book_num);
    bok->add_book(new_book);
    QMessageBox::information(this,tr("提示对话框"),QString("Successfully added! book name is %1").arg(bookname),QMessageBox::Ok);
    global::hash_new=new_book_num;
}


void single_book_add::on_adding_multi_book_clicked()
{
    QDate date = QDate::currentDate();

    QTime time=QTime::currentTime();
    QString totaltime=date.toString("MMdd")+time.toString("mmss");
    int hash=totaltime.toInt();
    QString hash_time= QString::number(hash+1);


    QString bookname=ui->bookName->text();
    QString book_author=ui->author->text();
    QString book_page=ui->pageNum->text();
    QString book_store=ui->storePlace->text();
    QString left_date =date.toString("yyyy-MM-dd");
    QString borrow = "allow";
    QString borrow_ID = "无人借阅";
    QString borrow_time = date.toString("yyyy-MM-dd");
    int bok_num=ui->multi_book_in->text().toInt();//一次添加了多少本书

    int new_borrow= hash_time.toInt();
    QString new_book_num= QString::number(new_borrow+1);

    if(bookname=="" || book_author=="" || book_page==""|| book_store=="")
       { QMessageBox::information(this,tr("提示对话框"),tr("书名，作者，页数或存书位置输入为空，请检查并重新输入！"), QMessageBox::Ok);
        return;
    }
    qDebug()<<new_book_num;
    for(int i=0;i<bok_num;i++)
    {
        for(int j=0;j<=i;j++)
        {
            new_borrow=new_borrow*new_borrow%totaltime.toInt();
            if(new_borrow<0)new_borrow=0-new_borrow;//负数时候取反
        }
        new_book_num= QString::number(new_borrow+1);
        book_info new_book (bookname,left_date,borrow,book_author,book_page,borrow_ID
                            ,book_store,borrow_time,new_book_num);
        bok->add_book(new_book);
    }
    global::hash_new=QString::number(bok_num);
    QMessageBox::information(this,tr("提示对话框"),QString("Successfully added! book name is %1,and num is %2")
                             .arg(bookname)
                             .arg(bok_num)
                             ,QMessageBox::Ok);
}
void single_book_add::processRet(){
    QLineEdit * returnPressedSender=(QLineEdit*)sender();
    if(returnPressedSender==ui->bookName)
        ui->author->setFocus();
    else if(returnPressedSender==ui->author){
        ui->pageNum->setFocus();
    }
    else if(returnPressedSender==ui->pageNum){
        ui->storePlace->setFocus();
    }
    else if(returnPressedSender==ui->storePlace){
        ui->multi_book_in->setFocus();
    }
    else if(returnPressedSender==ui->multi_book_in){
        ui->adding_multi_book->click();
    }


}
